import api from '@/core/Api'

const user = {
  namespaced: true,
  state () {
    return {
      current: {}
    }
  },
  actions: {
    getCurrentUser ({commit}) {
      api.get(window.$urls.getCurrentUser())
        .then((res) => {
          if (res.data.code === 0) {
            console.log(res.data.data)
            commit('setCurrentUser', res.data.data)
          } else {
            commit('setCurrentUser', {})
          }
        }).catch(() => {
          commit('setCurrentUser', {})
        })
    },
    clearCurrentUser ({commit}) {
      commit('clearCurrentUser')
    }
  },
  mutations: {
    /**
     * 当前用户
     */
    setCurrentUser (state, data) {
      state.current = data
    },
    /**
     * 当前用户
     */
    clearCurrentUser (state, data) {
      state.current = {}
    }
  },
  getters: {
    currentUser: state => {
      return state.current
    },
    authorities: state => {
      return state.current.authorities || []
      // return ['admin', 'user']
    }
  }
}

export default user
